
package controller;

import model.bean.BeanUsuario;
import model.dao.DaoUsuario;
import model.mail.Mail;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@WebServlet("/ServletPublico")
public class ServletPublico extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String accionStr = request.getParameter("accion") == null ? "7" : request.getParameter("accion");
            int accion = Integer.parseInt(accionStr);
            BeanUsuario bUsuario = null;

            DaoUsuario dUsuario = new DaoUsuario();
            Mail m = new Mail();
            String pagina = "";

            switch (accion) {

                case 1:
                    
                    String mail = String.valueOf(request.getParameter("textinput1"));
                    bUsuario = dUsuario.ValidarCorreo(mail);
                    
                    String remoteAddr = request.getRemoteAddr();


                    if (bUsuario != null) {

                        if (bUsuario.getCorreo().compareTo(mail) == 0) {

                                String token = Long.toHexString(Double.doubleToLongBits(Math.random()));
                               
                                dUsuario.setToken(token, bUsuario.getCorreo());

                                String link = "http://localhost:8084" + request.getContextPath() + "/ServletPublico?accion=3&link="+token;

                                m.enviacorreo(mail, link);
                                pagina = "/view/Contrasena/Enviado.jsp";

                        } 
                    } 
                    else {

                        pagina = "/index.jsp";
                    }
                    break;
                case 2:

                    String password = String.valueOf(request.getParameter("password1"));
                    String passwordR = String.valueOf(request.getParameter("password2"));
                    String idusuario = request.getParameter("vidusuario");

                    if (passwordR.compareTo(password) == 0) {


                        dUsuario.cambiarContraseña(Integer.parseInt(idusuario), passwordR);
                        pagina = "/index.jsp";

                    } else {

                        pagina = "/view/Contrasena/Reestablecer.jsp";
                    }


                    break;

                
                case 3:
                    String token = request.getParameter("link");
                    bUsuario = new BeanUsuario();
                    bUsuario = dUsuario.usuarioXToken(token);
                    if (bUsuario != null) {
                        token += "wxyz";
                        dUsuario.setToken(token, bUsuario.getCorreo());
                        request.setAttribute("busuario", bUsuario);
                        pagina = "/view/Contrasena/Reestablecer.jsp";
                    } else {
                        pagina = "/view/Contrasena/Usado.jsp";
                    }
                    break;
                    
                case 4:
                    pagina = "/index.jsp";
            }

            RequestDispatcher rd = getServletContext().getRequestDispatcher(pagina);
            rd.forward(request, response);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
